package com.caogt.org.orgInfo.controller;


import com.caogt.common.restful.ResultData;
import com.caogt.org.orgInfo.entity.OrgInfo;
import com.caogt.org.orgInfo.service.IOrgInfoService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author caogt
 * @since 2019-06-15
 */
@RestController
@RequestMapping("/orgInfo/org-info")
public class OrgInfoController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private IOrgInfoService orgInfoService;
    @PostMapping("/save")
    public ResultData save(@RequestBody OrgInfo orgInfo){
        orgInfoService.save(orgInfo);
        return ResultData.success();
    }
    @GetMapping("/query")
    public ResultData query(){
        return ResultData.success(orgInfoService.list());
    }
    @GetMapping("/getUsers")
    @HystrixCommand(fallbackMethod = "errorCallBack")
    public ResultData getUsers(String orgId){
        Object obj = restTemplate.getForObject("http://user/user/orgid/0",Object.class);
        return ResultData.success(obj);
    }

    /**
     * 微服务调用失败后熔断
     * @return
     */
    public ResultData errorCallBack(String orgId){

        return ResultData.fail("连接失败");
    }
}
